package org.darcy.framework.blog.core.schedule;

import java.util.List;

import org.darcy.entity.SysConfig;
import org.darcy.entity.SysLink;
import org.darcy.framework.blog.business.enums.ConfigKeyEnum;
import org.darcy.framework.blog.business.util.LinksUtil;
import org.darcy.service.SysConfigService;
import org.darcy.service.SysLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0.0
 * @since 1.0.0
 */
@Slf4j
@Component
public class FriendlyLinkTask {

	@Autowired
	private SysLinkService sysLinkService;
	@Autowired
	private SysConfigService sysConfigService;
	@Value("${app.enableAutoCheckLink}")
	private boolean enableAutoCheckLink;

	/**
	 * 每晚凌晨12点，检查友情链接
	 */
	@Scheduled(cron = "0 0 0 * * ?")
	public void check() {
		// 未开启自动检查友联的功能
		if (!enableAutoCheckLink) {
			return;
		}
		List<SysLink> linkList = sysLinkService.getList(null);
		if (CollectionUtils.isEmpty(linkList)) {
			return;
		}
		SysConfig sysConfig = sysConfigService.getByKey(ConfigKeyEnum.DOMAIN.getKey());
		if (StringUtils.isEmpty(sysConfig)) {
			return;
		}
		String domain = sysConfig.getConfigValue();
		for (SysLink link : linkList) {
			if (link.getStatus() > 0) {
				// 被禁用的网站如果又包含了本站链接，说明其已经恢复本站的友联
				// 因此自动恢复其友联
				if (LinksUtil.hasLinkByHtml(link.getUrl(), domain)
						|| LinksUtil.hasLinkByChinaz(link.getUrl(), domain)) {
					link.setStatus((byte) 1);
					link.setDescription("");
					sysLinkService.update(link);
				}
				continue;
			}

			// 已经不存在本站链接，自动下架该网站的友联
			if (!(LinksUtil.hasLinkByHtml(link.getUrl(), domain))
					&& !LinksUtil.hasLinkByChinaz(link.getUrl(), domain)) {
				link.setStatus((byte) 0);
				link.setDescription("系统检测到该网站已经取消本站的链接，因此自动封禁其友链");
				sysLinkService.update(link);
				log.info("系统监测到该网站（[{}]）已经私自取消本站链接，因此自动下架的友联", link.getName());
			}

		}
	}

}
